#include "Basic.h"
#include <Eigen>

int __c1;

class XSleeper : public QThread {
	void run() { }
public:
	static void sleep(unsigned long t) {
		QThread::msleep(t);
	}
};


void sleep(unsigned long t) {
	XSleeper::sleep(t);
}


QString deleteExt(QString s) {
	int lastDot = -1;
	int lastSlash = -1;
	for (int i = 0; i < s.size(); ++i) {
		QChar c = s.at(i);
		if (c.toAscii() == '.')
			lastDot = i;
		if (c.toAscii() == '\\')
			lastSlash = i;
		if (c.toAscii() == '/')
			lastSlash = i;
	}
	if (lastDot <= lastSlash)
		return s;
	return s.left(lastDot);
}


void solveEquation(double **_A, double *_b, double *_x, int n) {
	Eigen::MatrixXd A(n, n);
	Eigen::VectorXd b(n);
	//out << "solveEquation A(" << A.rows() << ", " << A.cols() << ")" << endl;
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < n; ++j)
			A(i, j) = _A[i][j];
	for (int i = 0; i < n; ++i)
		b(i) = _b[i];
		/*
	printf("A=\n");
	for(int i=0; i<n; ++i) {
		for(int j=0;j<n;++j)printf("%.3lf ",A(i,j));printf("\n");
	}
	printf("b=\n");
		for(int j=0;j<n;++j)printf("%.3lf ",b(j));printf("\n");
		*/

	Eigen::FullPivLU<Eigen::MatrixXd> solver(A);
	//out << "rank = " << solver.rank() << endl;
	Eigen::VectorXd x = solver.solve(b);
	//printf("x=\n");
		//for(int j=0;j<n;++j)printf("%.3lf ",x(j));printf("\n");
	for (int i = 0; i < n; ++i)
		_x[i] = x(i);
}


double sqr(double x) {
	return x * x;
}
